package com.tb.client.support.sso;

import com.tb.client.config.SsoProperties;
import com.tb.client.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Component
public class MySSOClientManager {

    @Autowired
    SsoProperties ssoProperties;

    public void toSSOLogin(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String redirectURL = ssoProperties.getServerUrl()+"/toLogin?redirectURL="+ssoProperties.getClientUrl()+request.getServletPath();
        response.sendRedirect(redirectURL);

    }


    public String verifyToken(String token) throws Exception {
        String redirectURL = ssoProperties.getServerUrl()+"/verify";
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("token",token);
        return HttpUtils.sendGetHttpUrl(redirectURL,paramMap);
    }

    public void loginCheck(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String redirectURL = ssoProperties.getServerUrl()+"/loginCheck?redirectURL="+ssoProperties.getClientUrl()+request.getServletPath();
        response.sendRedirect(redirectURL);
    }
}
